# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-mosquitto
pkgver=0.4.1
pkgrel=3
pkgdesc="Bindings to libmosquitto for Lua"
url="https://github.com/flukso/lua-mosquitto"
arch="all"
license="MIT"
makedepends="mosquitto-dev"
source="lua-mosquitto-$pkgver.tar.gz::https://github.com/flukso/lua-mosquitto/archive/v$pkgver.tar.gz
	checkinteger.patch
	0001-handle-lookup-errors-when-connecting.patch
	"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."
		make -C "$builddir-$lver" LUAPKGC=lua$lver LUA_VERSION=$lver
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Checking lua $lver build"
		(
		cd "$builddir-$lver"
		lua$lver -e 'print(require("mosquitto").version())'
		)
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$pkgver-1"

	cd "$builddir-$lver"
	msg "installig Lua $lver"
	make install DESTDIR="$subpkgdir" \
		LUA_LIBDIR=/usr/lib \
		LUAPKGC=lua$lver \
		LUA_VERSION=$lver

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
f94944d334142283006c3f079ced5284a1c41199fb9de7d7c4eb7c1a4fc412dc53f2f93cb5f08c54fec85c59fc8c2ee840111d4fbf0ff61a2254c8f83fa97e87  lua-mosquitto-0.4.1.tar.gz
d564b6ffbc1ed79cdaeb53c8cb94db946d697fa97a9e36a68ef5300a3f7f53eeb5216ddb5941cf8610b31d9f29f8ca697790431284842e87d1d85be8637a519b  checkinteger.patch
627ea37dfbfe2df93bfbd0d8918b1e0d0c8619c404b6009779dc2cf55dde519add76b0ca2320dfee7e1524b4ce89415523f8d327a97e7dceb575de2c92830793  0001-handle-lookup-errors-when-connecting.patch
"
